package com.java.comprehensive.arithmetic.leecode.medium;

import util.JsonUtil;
import util.ListNode;

public class LeetCode_707 {

    private static class MyLinkedList {

        private ListNode head;

        public MyLinkedList() {

        }

        public int get(int index) {
            ListNode dummyHead = new ListNode();
            dummyHead.next = head;
            ListNode current = dummyHead;
            int result = -1;
            while (current != null && index >= 0) {
                current = current.next;
                if (index == 0 && current != null) {
                    result = current.val;
                }
                index--;
            }
            return result;
        }

        public void addAtHead(int val) {
            ListNode curr = new ListNode(val);
            curr.next = head;
            head = curr;
        }

        public void addAtTail(int val) {
            if (head == null) {
                head = new ListNode(val);
                return;
            }
            ListNode dummyHead = new ListNode();
            dummyHead.next = head;
            ListNode curr = dummyHead;
            while (curr.next != null) {
                curr = curr.next;
            }
            curr.next = new ListNode(val);
        }

        public void addAtIndex(int index, int val) {
            ListNode dummyHead = new ListNode();
            dummyHead.next = head;
            ListNode curr = dummyHead;
            int n = 0;//设置阈值防止 index 比链表大
            while (curr.next != null && n < index) {
                curr = curr.next;
                n++;
            }
            if (n >= index) {
                ListNode temp = curr.next;
                curr.next = new ListNode(val);
                curr.next.next = temp;
                head = dummyHead.next;

            }
        }

        public void deleteAtIndex(int index) {
            ListNode dummyHead = new ListNode();
            dummyHead.next = this.head;
            ListNode curr = dummyHead;
            while (curr.next != null) {
                if (index == 0) {
                    curr.next = curr.next.next;
                } else {
                    curr = curr.next;
                }
                index--;
            }
            this.head = dummyHead.next;
        }
    }

    public static void main(String[] args) {
        //["MyLinkedList","addAtHead","deleteAtIndex","addAtHead","addAtHead","addAtHead","addAtHead","addAtHead","addAtTail","get","deleteAtIndex","deleteAtIndex"]
        //[[],[2],[1],[2],[7],[3],[2],[5],[5],[5],[6],[4]]
        //        ["MyLinkedList","addAtHead","addAtTail","addAtIndex","get","deleteAtIndex","get"]
        //[[],[1],[3],[1,2],[1],[0],[0]]
        MyLinkedList myLinkedList = new MyLinkedList();
        myLinkedList.addAtHead(1);
        myLinkedList.addAtTail(3);
        myLinkedList.addAtIndex(1, 2);
        System.out.println(myLinkedList.get(1));
        System.out.println(JsonUtil.toJson(myLinkedList));
    }
}
